package com.xs.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xs.common.JsonResult;
import com.xs.entity.Food;
import com.xs.entity.utils.CountAndDayCount;
import com.xs.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;


    /**
     * 新增
     * @param food
     * @return
     */
    @PostMapping
    public JsonResult<?> save(@RequestBody Food food){
        food.setTime(new Date());
        foodService.save(food);
        return JsonResult.success();
    }

    //根据id查询文章
    @GetMapping("/id/{id}")
    public JsonResult<?> getById(@PathVariable("id") Integer id){
        Food data = foodService.getById(id);
        data.setReadnumber(data.getReadnumber()+1);
        foodService.updateById(data);
        return JsonResult.success(data);
    }

    //点赞操作
    @PostMapping("/goodsave")
    public JsonResult<?> goodsave(@RequestBody Food food){
        foodService.updateById(food);
        return JsonResult.success();
    }

    //查询
    @GetMapping
    public JsonResult<?> getPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") Integer cover,
            @RequestParam(defaultValue = "") String name){
        QueryWrapper<Food> wrapper=new QueryWrapper<Food>();
        wrapper.orderByDesc("cover");
        if (StrUtil.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (cover!=null) {
            wrapper.eq("cover", cover);
        }
        Page<Food> page = foodService.page(new Page<>(pageNum, pageSize), wrapper);
        //解决删除操作导致的分页BUG
        if (pageNum>page.getPages()){
            page=foodService.page(new Page<>(page.getPages(),pageSize),wrapper);
        }
        return JsonResult.success(page);
    }

    /**
     * 修改
     * @param food
     * @return
     */
    @PutMapping
    public JsonResult<?> updateById(@RequestBody Food food){
        foodService.updateById(food);
        return JsonResult.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteById(@PathVariable Integer id){
        foodService.removeById(id);
        return JsonResult.success();
    }




    //获取当日食材数
    @GetMapping("/count")
    public JsonResult<?> countUser() throws ParseException {
        //获取总数量
        List<Food> row = foodService.list();
        List<String> time=new ArrayList<>();
        //创建时间转换器
        DateFormat df = new SimpleDateFormat("YYYY-MM-dd");
        for (Food user : row) {
            String format = df.format(user.getTime());
            time.add(format);
        }
        System.out.println(time);
        Date date = new Date();
        //获取当前时间的年月日
        String data = df.format(date);
        int count=0;

        for (String s : time) {
            if (s.equals(data)){
                count=count+1;
            }
        }
        //将数据打包
        CountAndDayCount rows=new CountAndDayCount();
        rows.setCount(foodService.count());
        rows.setDayCount(count);

        return JsonResult.success(rows);
    }
}
